<!DOCTYPE html>
<html>
<head>
<title>Created callback of a custom element should be invoked after custom element instance is created and its definition is registered</title>
<meta name="author" title="Sergey G. Grekhov" href="mailto:sgrekhov@unipro.ru">
<meta name="author" title="Aleksei Yu. Semenov" href="mailto:a.semenov@unipro.ru">
<meta name="assert" content="CREATED callback is invoked after custom element instance is created and its definition is registered">
<link rel="help" href="http://www.w3.org/TR/custom-elements/#types-of-callbacks">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../../testcommon.js"></script>
<link rel="stylesheet" href="/resources/testharness.css">
</head>
<body>
<div id="log"></div>
<script>
test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();
    var GeneratedConstructor = doc.registerElement('x-a', {prototype: proto});
    assert_equals(proto.createdCallbackCalledCounter, 0,
        'Callback created should not be called before element instance was created');
    var customElement = new GeneratedConstructor();
    assert_equals(proto.createdCallbackCalledCounter, 1,
        'Callback created should be called after element instance was created');
}, 'Test created callback when custom element is created by constructor');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();
    doc.registerElement('x-b', {prototype: proto});
    assert_equals(proto.createdCallbackCalledCounter, 0,
        'Callback created should not be called before element instance was created');
    doc.body.innerHTML = '<x-b></x-b>';
    assert_equals(proto.createdCallbackCalledCounter, 1,
        'Callback created should be called after element instance was created');
}, 'Test created callback when custom element is created in HTML');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();
    doc.registerElement('x-c', {prototype: proto});
    assert_equals(proto.createdCallbackCalledCounter, 0,
        'Callback created should not be called before element instance was created');
    doc.body.innerHTML = '<div><x-c></x-c></div>';
    assert_equals(proto.createdCallbackCalledCounter, 1,
        'Callback created should be called after element instance was created');
}, 'Test created callback when custom element is created in HTML as descendant of ' +
    'another element');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();
    doc.registerElement('x-d', {prototype: proto});
    assert_equals(proto.createdCallbackCalledCounter, 0,
        'Callback created should not be called before element instance was created');
    var customElement = doc.createElement('x-d');
    assert_equals(proto.createdCallbackCalledCounter, 1,
        'Callback created should be called after element instance was created');
}, 'Test created callback when custom element is created by createElement');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();

    doc.body.innerHTML = '<x-e></x-e>';
    doc.registerElement('x-e', {prototype: proto});
    assert_equals(proto.createdCallbackCalledCounter, 1,
        'Callback created should be called after element instance was created');
}, 'Test created callback when custom element is created in HTML before ' +
    'registration of a custom element');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();
    doc.registerElement('x-f', {prototype: proto});
    doc.body.innerHTML = '<x-f-unresolved id="x-f-unresolved"></x-f-unresolved>';
    assert_equals(proto.createdCallbackCalledCounter, 0,
        'Callback created should not be called if custom element is unresolved');

    var customElement = doc.querySelector('#x-f-unresolved');
    customElement.constructor.prototype = proto;
    assert_equals(proto.createdCallbackCalledCounter, 0,
        'Created callback should not be called if custom element is unresolved');
}, 'Test created callback if custom element is unresolved.');


testInIFrame('../../resources/x-element.html', function(doc) {
    var proto = newHTMLElementPrototype();
    doc.registerElement('x-element', {prototype: proto});
    assert_equals(proto.createdCallbackCalledCounter, 1, 'Callback created should be called');
}, 'Test created callback is called after custom element is created and registered. ' +
    'Document has browsing context');


testInIFrame('../../resources/register-and-create-custom-element.html', function(doc) {
    assert_equals(doc.querySelector('#log').innerText, 'Created callback was called',
        'Callback created should be called');
}, 'Test created callback is called after custom element is registered and created. ' +
    'Document has browsing context');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();
    var customElement = doc.createElement('x-g');

    doc.registerElement('x-g', {prototype: proto});
    assert_equals(proto.createdCallbackCalledCounter, 1,
        'Callback created should be called after registration of custom element');
}, 'Test created callback when custom element is created by createElement '+
    'before registration of a custom element');


test(function(){
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();
    var GeneratedConstructor = doc.registerElement('x-h', {prototype: proto});

    var customElement1 = new GeneratedConstructor();
    assert_equals(proto.createdCallbackCalledCounter, 1, 'Callback created should be called');

    var customElement2 = doc.createElement('x-h');
    assert_equals(proto.createdCallbackCalledCounter, 2,
        'Callback created should be called after element instance was created');

    doc.body.innerHTML = '<x-h></x-h>';
    assert_equals(proto.createdCallbackCalledCounter, 3,
        'Callback created should be called after element instance was created');

    doc.body.innerHTML = '<div><x-h></x-h></div>';
    assert_equals(proto.createdCallbackCalledCounter, 4,
        'Callback created should be called after element instance was created');
}, 'Test created callback. Create several custom elements using different ways');


testInIFrame('../../resources/x-element.html', function(doc) {
    var proto = newHTMLElementPrototype();

    var GeneratedConstructor = doc.registerElement('x-element', {prototype: proto});
    assert_equals(proto.createdCallbackCalledCounter, 1,
        'Callback created should be called for custom element in loaded document');

    var customElement2 = new GeneratedConstructor();
    assert_equals(proto.createdCallbackCalledCounter, 2,
        'Callback created should be called after element instance was created');

    var customElement3 = doc.createElement('x-element');
    assert_equals(proto.createdCallbackCalledCounter, 3,
        'Callback created should be called after element instance was created');

    doc.body.innerHTML = '<x-element></x-element>';
    assert_equals(proto.createdCallbackCalledCounter, 4,
        'Callback created should be called after element instance was created');

    doc.body.innerHTML = '<div><x-element></x-element></div>';
    assert_equals(proto.createdCallbackCalledCounter, 5,
        'Callback created should be called after element instance was created');
}, 'Test created callback. Create several custom elements using different ways. ' +
    'Document has browsing context');
</script>
</body>
</html>
